package com.enjoy.xbase.qk.model;


import com.enjoy.xbase.qk.TaskManger;
import com.enjoy.xbase.qk.impi.NetApi;
import com.enjoy.xbase.qk.impi.RequestBack;

import java.io.File;
import java.io.Serializable;
import java.util.HashMap;

/**
 * user请求参数
 * Created by cxb on 2016/4/13.
 */
public class CommonModel implements Serializable, NetApi {
    //标识当前请求是否是日志上传
    public boolean isLogUp = false;//log日志上传
    //请求标识id 用于取消操作
    private String tagId;
    private String url = "";
    private Method method = Method.POST;
    private ContentType contentType = ContentType.json;
    //请求数据
    private Object content;
    private HashMap<String, Object> parameter_obj;//装非文件数据的容器
    private HashMap<String, File> parameter_file;//装文件数据的容器

    private HashMap<String, String> headers;

    //设置请求信息
    private String tag;//请求附加信息，即请求id。相同id(实用于重复请求以最后一次有效，界面快速点击请求延时请求等)
    private long waitTime;//请求附加信息，即请求id。相同id(实用于重复请求以最后一次有效，界面快速点击请求延时请求等)
    private long requestTime;//请求时间


    public CommonModel() {
        this.parameter_obj = new HashMap<>();
        this.parameter_file = new HashMap<>();
        this.headers = new HashMap<>();
    }

    public void setTag(String tag, long waitTime) {
        this.tag = tag;
        this.waitTime = waitTime;
    }

    public String getTag() {
        return tag;
    }

    public long getWaitTime() {
        return waitTime;
    }

    public long getRequestTime() {
        return requestTime;
    }

    public Method getMethod() {
        return method;
    }

    public void setMethod(Method method) {
        this.method = method;
    }

    public ContentType getContentType() {
        return contentType;
    }

    public void setContentType(ContentType contentType) {
        this.contentType = contentType;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public boolean isLogUp() {
        return isLogUp;
    }

    public void setLogUp(boolean logUp) {
        isLogUp = logUp;
    }

    /**
     * 描述：添加文件
     *
     * @param key
     * @param file
     */
    public void put(String key, File file) {
        parameter_file.put(key, file);
    }

    /**
     * 描述：添加装非文件数据
     */
    public void put(String key, Object object) {
        parameter_obj.put(key, object);
    }

    public void putHeader(String key, String value) {
        headers.put(key, value);
    }

    public String getTagId() {
        return tagId;
    }

    public void setTagId(String tagId) {
        this.tagId = tagId;
    }

    public HashMap<String, Object> getParameter_obj() {
        return parameter_obj;
    }

    public HashMap<String, File> getParameter_file() {
        return parameter_file;
    }

    public Object getContent() {
        return content;
    }

    public void setContent(Object content) {
        this.content = content;
    }

    public HashMap<String, String> getHeaders() {
        return headers;
    }

    public void submit(RequestBack requestBack) {
        requestTime = System.currentTimeMillis();
        TaskManger.getInstance().request(this, requestBack);
    }

    public enum Method {
        GET, POST, DELETE, PUT
    }

    public enum ContentType {
        json, form
    }
}
